//
//  HYFComposeTabBarView.swift
//  MyWeibo
//
//  Created by 黄勇锋 on 16/7/27.
//  Copyright © 2016年 黄勇锋. All rights reserved.
//compose_add_background_highlighted

import UIKit

enum HYFComposeTabBarViewType:Int {
    
    case Picture = 1001
    case Mention = 1002
    case Trend = 1003
    case Emotion = 1004
    case Add = 1005
    
}

class HYFComposeTabBarView: UIView {
    
    //表情按钮
    var emoticonButton:UIButton?
    
    //是否是表情键盘
    var isEmoticon:Bool = false{
        
        didSet{
            
            let imgName = isEmoticon ? "compose_keyboardbutton_background" :"compose_emoticonbutton_background"
            
            emoticonButton?.setImage(UIImage(named: imgName), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "\(imgName)_highlighted"), forState: .Highlighted)
            
        }
    }
    
    //定义一个闭包
    var closure:((type:HYFComposeTabBarViewType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func setupUI(){
        
        backgroundColor = RandomColor()
        
        //添加控件,设置约束
        let pictureButton = addChildButton("compose_toolbar_picture", type: .Picture)
        let mentionButton = addChildButton("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButton("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background",type: .Emotion)
        let addButton = addChildButton("compose_add_background",type: .Add)
        
        //添加约束
        pictureButton.snp_makeConstraints { (make) in
            
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        
        mentionButton.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        
        trendButton.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton!)
        }
        
        emoticonButton!.snp_makeConstraints { (make) in
            
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) in
            
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton!.snp_trailing)
        }
        
    }
    
    func addChildButton(imgName:String,type:HYFComposeTabBarViewType) -> UIButton{
        
        let button = UIButton()
        
        //给 button.tag 的属性赋值  --> 将枚举值转为 Int 类型
        button.tag = type.rawValue
        
        button.setImage(UIImage(named: imgName), forState: .Normal)
        button.setImage(UIImage(named: "\(imgName)_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Highlighted)
        
        addSubview(button)
        
        //添加点击事件
        button.addTarget(self, action: #selector(HYFComposeTabBarView.buttonClick(_:)), forControlEvents: .TouchUpInside)
        
        return button
    }
    
    
    func buttonClick(button:UIButton){
        
        //将 Int 类型转为枚举值
        closure?(type:HYFComposeTabBarViewType(rawValue:button.tag)!)
    }

}
